import java.util
        .concurrent.ExecutorService;


import java.util
        .concurrent.Executors;

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author shilinghuai
 * @create 2022/5/30
 * @since 1.0.0
 */
public class ThreePrintT {
    static String wakeFlag ="A";
    public static void main(String[] args){
        Object o  = new Object();
        MyThread A = new MyThread("A",o);
        MyThread B = new MyThread("B",o);
        MyThread C = new MyThread("C",o);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.execute(A);
        executorService.execute(B);
        executorService.execute(C);
    }
    public static class MyThread implements Runnable{
        public int countA = 0;
        public int countB  = 0;
        public int countC = 0;
        public  String workFlag ="A";
        public Object o=null;
        public MyThread(String workFlag,Object o){
            this.workFlag = workFlag;
            this.o = o;
        }
        @Override
        public
        void run() {
            if("A".equals(workFlag)){
                while (countA++<10){
                    synchronized (o){
                        while (!"A".equals(wakeFlag)){
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("A");
                        wakeFlag="B";
                        o.notifyAll();

                    }
                }

            }else if("B".equals(workFlag)){
                while (countB++<10){
                    synchronized (o){
                        while (!"B".equals(wakeFlag)){
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("B");
                        wakeFlag= "C";
                        o.notifyAll();
                    }
                }

            }else if("C".equals(workFlag)){
                while (countC++<10){
                    synchronized (o){
                        while (!"C".equals(wakeFlag)){
                            try {
                                o.wait();
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }
                        System.out.println("C");
                        wakeFlag="A";
                        o.notifyAll();
                    }
                }


            }
        }
    }
}
